package cn.itcast.controller;

import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.service.AssessmentService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/assessment")
public class AssessmentController {

    @Reference
    AssessmentService assessmentService;


    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean bean){
        return assessmentService.findPage(bean);
    }


    @RequestMapping("/add")
    public Result add(@RequestBody Object[] obj){
       try {
           String fileNumber = JSON.parseObject(obj[0].toString(), String.class);
           Map sun = JSON.parseObject(obj[1].toString(), Map.class);
           if (sun.size()<7) {
               return new Result(false,"表卷存在未填写,请核对后再提交" );
           }
           Map mentality = JSON.parseObject(obj[2].toString(), Map.class);
           if (mentality.size()<10) {
               return new Result(false,"表卷存在未填写,请核对后再提交" );
           }
               assessmentService.add(fileNumber,sun,mentality);
               return new Result(true,"健康测评表卷填写完毕");

       }catch (Exception e){
           e.printStackTrace();
           return new Result(false,"健康测评表卷填写失败" );
       }
    }

    @RequestMapping("/findAll")
    public Result findAll(@RequestBody Object[] obj){
        try {
            String fileNumber = JSON.parseObject(obj[0].toString(), String.class);
            List<Map<String,Object>> list = assessmentService.findAllByfileNumber(fileNumber);
            return new Result(true, "测评报告查询成功",list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"测评报告查询失败,或者未作测评报告" );
        }
    }

    @RequestMapping("/delete")
    public Result delete(String fileNumber){
        try {
            assessmentService.deleteByFileNumber(fileNumber);
            return new Result(true,"该用户的测评问卷删除成功" );
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"该用户的测评问卷删除失败" );
        }
    }

}
